﻿using AvaMaid.Messages;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZetaIpc.Runtime.Server;

namespace AvaMaid.Services
{
    public class IPCService
    {
        private IpcServer _server;

        public IPCService()
        {
            _server = new IpcServer();

        }

        public void Start()
        {
            _server.Start(7609);
            _server.ReceivedRequest += S_ReceivedRequest;
        }

        public void Close()
        {
            _server.Stop();
            _server.ReceivedRequest -= S_ReceivedRequest;
        }

        private void S_ReceivedRequest(object sender, ReceivedRequestEventArgs e)
        {
            Task.Run(() =>
            {
                WeakReferenceMessenger.Default.Send(new IPCParamsEventArgs() { Args = e.Request.Split(',') });
                e.Handled = true;
            });

        }

        public void SendLazyMessage(string text)
        {
            Task.Run(() =>
            {

                Thread.Sleep(500);
                WeakReferenceMessenger.Default.Send(new IPCParamsEventArgs() { Args = text.Split(',') });
            });


        }

    }


    public static class IPCServiceRegister
    {

    }
}
